package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/logout")
    //需要将session中的"employee"移除
    public R Logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping("/login")
    public R Login(@RequestBody Employee em, HttpServletRequest request){
        String pwd = DigestUtils.md5DigestAsHex(em.getPassword().getBytes());
        em.setPassword(pwd);

        Employee employee=employeeService.login(em);
        if(employee==null){
            return R.error("登录失败");
        }
        if (employee.getStatus()==0){
            return R.error("账号已禁用");
        }
        HttpSession session = request.getSession();
        session.setAttribute("employee",employee.getId());
        return R.success(employee);
    }

    @PostMapping
    public R save(@RequestBody Employee employee,HttpServletRequest request){
        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils. md5DigestAsHex("123456".getBytes()));

        //现在这些公共字段全部交给MetaObjectHandler来赋值
        /*employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());

        //获得当前登录用户的id
        Long empId =(Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        boolean save = employeeService.save(employee);

        return R.success("添加成功");

        //添加失败时采用异常处理
    }

    @GetMapping("/page")
    public R<Page> pageR(@RequestParam Integer page,
                         @RequestParam Integer pageSize,
                         @RequestParam(required = false,defaultValue = "") String name){
        System.out.println("page = " + page + ", pageSize = " + pageSize + ", name = " + name);
        Page<Employee> employeePage = new Page<>(page,pageSize);
        return employeeService.pageR(employeePage,name);
    }

    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        /*//还需要修改 updateTime和修改人
        employee.setUpdateTime(new Date());
        employee.setUpdateUser((long)request.getSession().getAttribute("employee"));*/

        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    /**
     * 这实际上是个编辑(修改)员工信息的方法
     * 但这是回显信息的功能，而修改的功能前端会在接收此方法返回值后调用上面的update方法
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee em = employeeService.getById(id);
        if(em!=null){
            return R.success(em);
        }
        return R.error("没有查询到该员工数据");
    }
}
